package com.Akronmcp.hta.main;

import java.io.File;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import com.Akronmcp.hta.obj.HDancer;
import com.Akronmcp.hta.obj.HManager;
import com.Akronmcp.hta.obj.HopHolo;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketAdapter;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;

import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;

/**主类*/
public class HM extends JavaPlugin 
{
	
	public static FileConfiguration conf;
	public static FileConfiguration lang;
	public static HManager hta;
    
	public static File df;
    
	public static JavaPlugin ins;
	
//    public static PacketAdapter lp1; 
  
    public static ProtocolManager pm;
	  //获得插件protocolapi类,指向各种方法
    
    public static Economy econ = null;
    //vault经济实例
    public static boolean isVault = false;
    
    public static Permission perm = null;
    //vault权限实例
    
    public static boolean papiloaded = Bukkit.getPluginManager().getPlugin("PlaceholderAPI")!=null;
    public static boolean hololoaded = Bukkit.getPluginManager().getPlugin("HolographicDisplays")!=null;
   //是否已加载papi
    
    @Override
	public void onEnable()////////////////
	{
    	ins = this;
    	
    	//_初始化1
    	 hta = new HManager();
         pm = ProtocolLibrary.getProtocolManager();
         HDancer.loadPreDancer();
     
    	 df = getDataFolder();
    	 
    	 if(!df.exists())
    	 {
    	   File filelangf=new File(df,"lang");
    	   getDataFolder().mkdir(); 
    	   filelangf.mkdir();
    	   
    	   try
	      	 {
	      	  saveResource("config.yml", false);
	      	  saveResource("a/hopchat1.yml", false);
	      	  
	      	  saveResource("a/hoptitle1.yml", false);
	      	  saveResource("b/hoptitle2.yml", false);
	      	  
	      	  saveResource("a/hopbar1.yml", false);
	      	  saveResource("b/hopbar2.yml", false);
	      	  
	      	  saveResource("a/hopbook1.yml", false);
	      	  saveResource("b/hopholo.yml", false);
	      	  
	      	  saveResource("b/hopmodules.yml", false);
	      	  
	      	  saveResource("食用方法(help).yml", false);
	      	  saveResource("lang/ZH_CN.yml", false);
	      	  //读取配置
	      	 }catch(Exception e)
	      	 {
	      		 e.printStackTrace();
	      	 }
	      	 
    	   
    	 }
    	 //创建插件数据目录and lang
    	
    	 
    	 //if (!(fileconf.exists())) {saveDefaultConfig();}
         //读取jar的config
    	  
    	 if(Bukkit.getPluginManager().getPlugin("Vault")!=null)
    	 {
    		 isVault = setupEconomy(); 
    		 setupPermissions();
    	 }
    	   
    	  try {
			
    		  HLoader.reload_conf(hta);
    		  //重载
    		  
				System.out.println(HComer.help("loadok"));

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(HComer.help("loadnotok"));
 			}
    	   
    	  
    	  
    	  Metrics metrics = new Metrics(this);//实例化bstat
    	  metrics.addCustomChart(new Metrics.SimplePie("hta_users", () -> "1"));
    	  //添加自定义表格
    	  
  		  
    	  
    	  //getServer().getPluginManager().registerEvents(new HListener(), (Plugin)this);
    	  //注册监听器
    	  
    	 
    	  CommandExecutor comer = new HComer();
          getCommand("hta").setExecutor(comer);
          getCommand("htab").setExecutor(comer);
          getCommand("htas").setExecutor(comer);

          getCommand("hta").setTabCompleter((TabCompleter) comer);
          getCommand("htab").setTabCompleter((TabCompleter) comer);
          getCommand("htas").setTabCompleter((TabCompleter) comer);

    	  //getCommand("hta").setExecutor(this);
    	  //注册指令执行器
    	  
    	 
          
          //HListener.lpl1(this,lp1);
          //启用pl监听器
        
          
    	getLogger().info("HTA已启动");
		
    	
	}
	
    //启动vault经济系统
    private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return false;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            return false;
        }
        econ = rsp.getProvider();
        return econ != null;
    }
    
  //启动vault权限系统
    private boolean setupPermissions()
    {
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            perm = permissionProvider.getProvider();
        }
        return (perm != null);
    }
   
   
	@Override
	public void onDisable()////////////////
	{
		
//		pm.getPacketListeners().remove(lp1);
		
//    	HandlerList.unregisterAll();
    	//注销监听事件
		
    	hta.clearAll();
	}



}
